# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=gdb
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-multiarch")
pkgver=13.2
pkgrel=3
pkgdesc="GNU Debugger (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.gnu.org/software/gdb/"
license=('spdx:GPL-3.0-or-later')
groups=($( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-toolchain" ))
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-mpc"
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         "${MINGW_PACKAGE_PREFIX}-ncurses"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-xxhash"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-pygments: for syntax highlighting")
checkdepends=('dejagnu' 'bc')
# gmp, mpfr and xz (lzma) are linked statically
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-iconv"
             "${MINGW_PACKAGE_PREFIX}-xz")
source=(https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz{,.sig}
        '0001-Workaround-performance-regression-in-info-func-var-t.patch'
        '0002-Fix-using-gnu-print.patch'
        '0003-configure-Disable-static-linking-with-standard-libs.patch'
        '0004-Python-Configure-path-fixes.patch'
        '0005-W32-Always-check-USERPROFILE-if-HOME-is-not-set.patch'
        '0006-Handle-unwinding-from-SEGV-on-Windows.patch')
validpgpkeys=('F40ADB902B24264AA42E50BF92EDB04BFF325CF3')
sha256sums=('fd5bebb7be1833abdb6e023c2f498a354498281df9d05523d8915babeb893f0a'
            'SKIP'
            '428cda7dcc107c236225ae89704390b0f1e70c276b5ca6b1741988f384b21d62'
            'cf12bce0b988765ecac26a6895238c48ef9676d6fc01f986f75dcbc8bd8d3f07'
            '0eb291cd81f7392610d16a83f436a30d3384a99661c6b6ffd1bfb243d5aee6dd'
            '7ef9c6e238a4e232bc689e15e48ee2d8045542c47f0b156d9fc92a7c14e6757e'
            '39d1cb2a1be8d60c16404ad96882f10cd3ebd942d8b7af62a7416a230a50de93'
            '5d0086ed36649c8faeb46efe5aeaf9ca6b6602a8d90ea72e1feb2436b1396acc')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  # https://sourceware.org/bugzilla/show_bug.cgi?id=15412
  apply_patch_with_msg \
    0001-Workaround-performance-regression-in-info-func-var-t.patch

  apply_patch_with_msg \
    0002-Fix-using-gnu-print.patch

  # https://sourceware.org/bugzilla/show_bug.cgi?id=21078
  apply_patch_with_msg \
    0003-configure-Disable-static-linking-with-standard-libs.patch

  apply_patch_with_msg \
    0004-Python-Configure-path-fixes.patch \
    0005-W32-Always-check-USERPROFILE-if-HOME-is-not-set.patch

  # https://sourceware.org/bugzilla/show_bug.cgi?id=30255
  apply_patch_with_msg \
    0006-Handle-unwinding-from-SEGV-on-Windows.patch

  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure
}

do_build() {
  builddir=${srcdir}/build-${MSYSTEM}$1
  mkdir ${builddir} && cd ${builddir}

  if [ "${CARCH}" != "x86_64" ]; then
    LDFLAGS+=" -Wl,--large-address-aware"
  fi

  if check_option "debug" "y"; then
    CFLAGS+=" -O0"
    CXXFLAGS+=" -O0"
  fi

  CPPFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  CFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"

  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    $2 \
    --enable-64-bit-bfd \
    --disable-werror \
    --disable-win32-registry \
    --disable-rpath \
    --disable-sim \
    --with-curses \
    --with-system-gdbinit=${MINGW_PREFIX}/etc/gdbinit \
    --with-system-readline \
    --with-system-zlib \
    --with-python=${MINGW_PREFIX}/bin/python \
    --with-expat \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    --with-mpfr \
    --with-lzma \
    --with-zstd \
    --enable-tui \
    --enable-source-highlight=no

  make
}

build() {
  do_build
  do_build -multiarch --enable-targets=all
}

package_gdb() {
  cd ${srcdir}/build-${MSYSTEM}
  make DESTDIR=${pkgdir} install

  # Remove unwanted files
  rm -rf ${pkgdir}${MINGW_PREFIX}/share/{man,info}

  rm -f ${pkgdir}${MINGW_PREFIX}/include/*.h
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/*.a
}

package_gdb-multiarch() {
  pkgdesc="GNU Debugger (supports all targets)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}")

  builddir=${srcdir}/build-${MSYSTEM}-multiarch
  cd ${builddir}
  make DESTDIR=${builddir}/inst install-gdb install-gdbserver

  destdir=${pkgdir}${MINGW_PREFIX}/bin
  bindir=${builddir}/inst${MINGW_PREFIX}/bin
  mkdir -p $destdir
  cp -p ${bindir}/gdb.exe ${destdir}/gdb-multiarch.exe
  cp -p ${bindir}/gdbserver.exe ${destdir}/gdbserver-multiarch.exe
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
